echo "===================== OpenStack Deploy Model ======================="
echo "                                                                    "
echo "                      (Internet)      (Managment Network)           "
echo "                          |                     |                   "
echo "                    -------------+--           ---+------------     "
echo "                                 |                |                 "
echo "                             +--eth1-------------eth0---+           "
echo "                             |                          |           "
echo "                             |  Nova-API, Nova-Conductor|           "
echo "                             |  Nova Scheduler          |           "
echo "                             |  Neutron Server          |           "
echo "                             |  Cinder-API,Cinder-volume|           "
echo "                             +--------------------------+           "
echo "                                                                    "
echo "                                                                    "
echo "Usage:openstack_install.sh <managemnt IP>                           "
echo "      ex) openstack_install.sh 192.168.1.1                          "
echo "Are you specify host IP($1)?                                        "
echo "===================================================================="

sleep 5

apt-get update

echo "The number of arguments is: $#"

if [ $# -eq 1 ]
then
        echo "success"
else
        echo "fail"
        exit
fi

echo "going..."

msg="s/127.0.1.1/$1/g"
sed -i $msg /etc/hosts

echo "
net.ipv4.ip_forward=1
net.ipv4.conf.all.rp_filter=0
net.ipv4.conf.default.rp_filter=0
" >> /etc/sysctl.conf

echo "Install MySQL Server"
sudo apt-get install -q -y python-mysqldb mysql-server
sudo sed -i 's/127.0.0.1/0.0.0.0/g' /etc/mysql/my.cnf
sudo service mysql restart

echo "Update OpenStack DB"
mysql -u root -p <<EOF
CREATE DATABASE nova;
GRANT ALL PRIVILEGES ON nova.* TO 'nova'@'%' \
IDENTIFIED BY 'nova_pass';
CREATE DATABASE glance;
GRANT ALL PRIVILEGES ON glance.* TO 'glance'@'%' \
IDENTIFIED BY 'glance_pass';
CREATE DATABASE keystone;
GRANT ALL PRIVILEGES ON keystone.* TO 'keystone'@'%' \
IDENTIFIED BY 'keystone_pass';
CREATE DATABASE neutron;
GRANT ALL PRIVILEGES ON neutron.* TO 'neutron'@'%' \
IDENTIFIED BY 'neutron_pass';
CREATE DATABASE cinder;
GRANT ALL PRIVILEGES ON cinder.* TO 'cinder'@'%' \
IDENTIFIED BY 'cinder_pass';
GRANT ALL PRIVILEGES ON nova.* TO 'nova'@'localhost' \
IDENTIFIED BY 'nova_pass';
GRANT ALL PRIVILEGES ON glance.* TO 'glance'@'localhost' \
IDENTIFIED BY 'glance_pass';
GRANT ALL PRIVILEGES ON keystone.* TO 'keystone'@'localhost' \
IDENTIFIED BY 'keystone_pass';
GRANT ALL PRIVILEGES ON neutron.* TO 'neutron'@'localhost' \
IDENTIFIED BY 'neutron_pass';
GRANT ALL PRIVILEGES ON cinder.* TO 'cinder'@'localhost' \
IDENTIFIED BY 'cinder_pass';
FLUSH PRIVILEGES;
EOF

echo "Install Rabbit Server"
sudo apt-get install -y rabbitmq-server
sudo rabbitmqctl change_password guest rabbit_pass


echo "Install Keystone"
sudo apt-get install -y keystone python-keystone python-keystoneclient
rm /etc/keystone/keystone.conf
wget -O /etc/keystone/keystone.conf https://conf.googlecode.com/svn/trunk/openstack/keystone.conf

sudo service keystone restart
sudo keystone-manage db_sync

echo "Update openrc"
wget -O /root/openrc https://conf.googlecode.com/svn/trunk/openstack/openrc

source ~/openrc
echo "source ~/openrc" >> ~/.bashrc

echo "Update keystone-init"
rm /root/keystone-init
wget -O /root/keystone-init https://conf.googlecode.com/svn/trunk/openstack/keystone-init
msg="s/localhost/$1/g"
sudo sed -i $msg /root/keystone-init

chmod 755 ~/keystone-init
~/keystone-init

echo "Install glance"
sudo apt-get install -y glance glance-api glance-registry python-glanceclient glance-common
rm /etc/glance/glance-api.conf
wget -O /etc/glance/glance-api.conf https://conf.googlecode.com/svn/trunk/openstack/glance-api.conf
msg="s/%SERVER%/$1/g"
sudo sed -i $msg /etc/glance/glance-api.conf

rm /etc/glance/glance-registry.conf
wget -O /etc/glance/glance-registry.conf https://conf.googlecode.com/svn/trunk/openstack/glance-registry.conf
sudo sed -i $msg /etc/glance/glance-registry.conf

sudo service glance-api restart && sudo service glance-registry restart
sudo glance-manage db_sync

sudo apt-get install -y nova-api nova-cert nova-common nova-conductor nova-scheduler python-nova python-novaclient nova-consoleauth novnc nova-novncproxy 
sudo apt-get install -y cinder-api cinder-scheduler cinder-volume python-cinderclient

sudo sed -i 's/%SERVICE_TENANT_NAME%/service/g' /etc/nova/api-paste.ini
sudo sed -i 's/%SERVICE_USER%/nova/g' /etc/nova/api-paste.ini
sudo sed -i 's/%SERVICE_PASSWORD%/service_pass/g' /etc/nova/api-paste.ini

rm /etc/nova/nova.conf
wget -O /etc/nova/nova.conf https://conf.googlecode.com/svn/trunk/openstack/nova.conf
msg="s/%SERVER%/$1/g"
sudo sed -i $msg /etc/nova/nova.conf

sudo nova-manage db sync

echo "Install Neutron"
sudo apt-get install -y openvswitch-controller openvswitch-switch openvswitch-datapath-source
sudo apt-get install -y neutron-server neutron-dhcp-agent neutron-plugin-openvswitch-agent neutron-l3-agent
rm /etc/neutron/neutron.conf
wget -O /etc/neutron/neutron.conf https://conf.googlecode.com/svn/trunk/openstack/neutron.conf

rm /etc/neutron/api-paste.ini
wget -O /etc/neutron/api-paste.ini https://conf.googlecode.com/svn/trunk/openstack/neutron/api-paste.ini
msg="s/%SERVER%/$1/g"
sudo sed -i $msg /etc/neutron/api-paste.ini


rm /etc/neutron/dhcp_agent.ini
wget -O /etc/neutron/dhcp_agent.ini https://conf.googlecode.com/svn/trunk/openstack/neutron/dhcp_agent.ini

rm /etc/neutron/plugins/openvswitch/ovs_neutron_plugin.ini
wget -O /etc/neutron/plugins/openvswitch/ovs_neutron_plugin.ini https://conf.googlecode.com/svn/trunk/openstack/neutron/ovs_neutron_plugin.ini

ovs-vsctl add-br br-int
ovs-vsctl add-br br-eth1
#ovs-vsctl add-port br-eth1 eth1


sudo apt-get install -y openstack-dashboard memcached python-memcache
sudo apt-get remove -y --purge openstack-dashboard-ubuntu-theme

service nova-api restart && service nova-cert restart && service nova-consoleauth restart && \
service nova-scheduler restart && service nova-novncproxy restart && service nova-conductor restart && \
service cinder-api restart && service cinder-scheduler restart 

service neutron-server restart 
service neutron-plugin-openvswitch-agent restart
service neutron-dhcp-agent restart
service neutron-l3-agent restart

neutron net-create sharednet1 --shared --provider:network_type flat --provider:physical_network physnet1
neutron subnet-create sharednet1 192.168.101.0/24
